# -*- coding: utf-8 -*-
import scrapy
class Item(dict):
    class_name = ''

    def __init__(self, selector, class_name=''):
        super().__init__()
        self._selector = selector
        self.class_name = class_name

    def add_path(self, attr, xpath, begin=0, end=None):
        if end is None:
            self[attr] = self._selector.xpath(xpath).get(begin)
            return
        self[attr] = self._selector.xpath(xpath).getall()[begin:end + 1]
# 爬取各个品牌的logo src及其名字
class CarBranImgSpider(scrapy.Spider):
    name = 'car-brand-img'
    start_urls = ['http://db.auto.sohu.com/brand_191/home/iframe.html']

    # 访问每个品牌的主页
    def parse(self, response):
        i = 191
        n = 279
        while i <= n:
            url = 'http://db.auto.sohu.com/brand_' + str(i) + '/home/iframe.html'
            i += 1
            yield scrapy.Request(url, callback=self.solve_detail)
        # for e in response.xpath('//li[@class="close_child"]'):
        #     brand=e.xpath('h4[@class="brand_tit"]//a/text()').extract()[0].split(',')[1].replace('\'','').strip()

    # 访问每个车系的主页，清除掉未上市或者停售的车系
    def solve_detail(self, response):
        res=response.xpath('//div[@class="tit brandtit"]//img')
        src='http:'+res.xpath('@src').extract()[0]
        if src=='http:':
            return
        alt=res.xpath('@alt').extract()[0]
        item=Item('')
        item['src']=src
        item['alt']=alt
        yield item
